﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using static Loglib.LogBase;

namespace Loglib
{
    /// <summary>
    /// 消息的格式
    /// </summary>
    public class LogBase
    {
        public string _Msg;
        public LogLevel _level;

        public LogBase(string Msg, LogLevel level)
        {
            _Msg = Msg;
            _level = level;
        }

        public enum LogLevel
        {
            Information = 0,
            warrning,
            error
        }
    }
    /// <summary>
    /// 日志窗体类
    /// </summary>
    public class LogView
    {
        private static LogView _instance = null;
        private static readonly object InitLock = new object();
        private static List<TextBoxBase> _logList = null;

        private LogView()
        {
            _logList = new List<TextBoxBase>();
        }
        private static LogView Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (InitLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new LogView();
                        }
                    }
                }
                return _instance;
            }
        }
        /// <summary>
        /// 窗体注册类
        /// </summary>
        /// <param name="logBox">消息</param>
        /// <returns>true添加成功，false不添加</returns>
        private bool AddLogView(TextBoxBase logBox)
        {
            if (_logList.Contains(logBox))
            {
                return false;
            }
            _logList.Add(logBox);
            return true;
        }
        private bool RemovelogView(TextBoxBase logBox)
        {
            if (_logList.Count == 0 || !_logList.Contains(logBox))
            {
                return false;
            }
            _logList.Remove(logBox);
            return true;
        }
        /// <summary>
        /// 实现委托
        /// </summary>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        private bool Toast(LogBase msg)
        {
            if (_logList != null)
            {
                _logList.ForEach(logControl => logControl.Invoke(new Action(() =>
                {
                    if (logControl.Lines.Length > 100)
                    {
                        logControl.Text = @"";
                    }
                    logControl.Text += msg._Msg;
                    logControl.Select(logControl.Text.Length, 0);
                    logControl.ScrollToCaret();
                })));
            }
            return true;
        }
        public static bool ToastLog(LogBase log)
        {
            return Instance.Toast(log);
        }

        public static bool AddLogBox(TextBoxBase logBox)
        {
            return Instance.AddLogView(logBox);
        }

        public static bool RemoveLogBox(TextBoxBase logBox)
        {
            return Instance.RemovelogView(logBox);
        }
        /// <summary>
        /// 实时打印日志
        /// </summary>
        public static class log
        {
            /// <summary>
            /// 调用此方法实现打印
            /// </summary>
            /// <param name="Msg">消息</param>
            /// <param name="level">等级</param>
            public static void Pop(string Msg, LogLevel level)
            {

                LogBase msgEntry = new LogBase(Msg + Environment.NewLine, (LogLevel)(int)level);
                string log = String.Format(@"{0:mm:ss:} {1}: {2}", DateTime.Now,
                    msgEntry._level,
                    msgEntry._Msg);
                LogView.ToastLog(msgEntry);
            }
        }


    }

}
